#ifndef __CONFIGURATION_HPP__
#define __CONFIGURATION_HPP__

#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <set>
#include <fstream>

using namespace std;
//单例类获取配置路径
class Configuration
{
public:
    static Configuration* getInstance();//获取唯一实例
    static void destroy();//销毁接口
    map<string,string>& getConfigMap();//获取存放配置文件内容的map
    set<string> getStopWordList();//获取停用词词集

private:
    Configuration(const string& filepath);//构造私有
    ~Configuration();//析构私有
    void readConfig(const string & filepath);//读配置文件,写到_configMap中
    void readStopWords(const string & filepath);//读停用词,写到_stopWordList

private:
    static Configuration* _pInstance;
    string _filepath;//配置文件路径
    map<string,string> _configMap;//配置文件内容
    set<string> _stopWordList;//停用词词集
};

Configuration::Configuration(const string& filepath)
: _filepath(filepath)
{
    readConfig(filepath);
    readStopWords(_configMap["stop_words.utf8"]);
}

Configuration::~Configuration()
{
    destroy();
}

void Configuration::readConfig(const string & filepath){
    ifstream ifs(filepath);
    ifs.seekg(0);
    if(!ifs.good()){
        cerr << ">> ifstream open file error!\n";
        return;
    }
    string line;
    while(getline(ifs,line)){
        istringstream iss(line);
        string key,value;
        iss>>key>>value;
        _configMap[key]=value;
    }
    ifs.close();
}

void Configuration::readStopWords(const string & filepath){
    ifstream ifs(filepath);
    ifs.seekg(0);
    if(!ifs.good()){
        cerr << ">> ifstream open file error!\n";
        return;
    }
    string word;
    while(!ifs.eof()){
        ifs >> word;
        _stopWordList.insert(word);
    }
    ifs.close();
}

Configuration* Configuration::getInstance()
{
    if(!_pInstance){
        _pInstance = new Configuration("/home/hcg/CPP2/SE_v1/SE/conf/settings.conf");
    }
    return _pInstance;
}

void Configuration::destroy(){
    if(_pInstance){
        delete _pInstance;
        _pInstance = nullptr;
    }
}

map<string,string> & Configuration::getConfigMap()
{
    return _configMap;
}

set<string> Configuration::getStopWordList()
{
    return _stopWordList;
}


#endif


